package cn.laixueit.utils;

import cn.laixueit.pojo.Book;
import cn.laixueit.pojo.BookChapter;
import cn.laixueit.pojo.BookChapterContent;
import cn.laixueit.service.BookService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author duxiang
 * @date 2023/12/18 14:08
 */
@Component
@Data
public class ExportWordUtils {

    @Resource
    private BookService bookService;

    /**
     * 创建需要写出的word文旦的内容格式
     [
         {"java基础":
             [
                {"章节1"："内容1"}
             ]
         }
     ]
     * @return
     */
    public LinkedList<Map<String,List>> createWordText(List<String> ids){
        LinkedList<Map<String, List>> linkedList = new LinkedList<>();
        for (String id : ids) {
            Book book = bookService.listBookAndChaptersAndContents(id);
            LinkedList<BookChapter> chapterList = book.getChapterList();
            List<LinkedHashMap> list = chapterList.stream().map(x -> {
                LinkedHashMap map = new LinkedHashMap();
                map.put(x.getName(),Optional.ofNullable(x.getBookChapterContent()).orElse(new BookChapterContent()).getContent());
                return map;
            }).collect(Collectors.toList());
            HashMap<String, List> map = new LinkedHashMap<>();
            map.put(book.getName(),list);
            linkedList.add(map);
        }
        return linkedList;
    }
}
